const asyncFun = (name) => new Promise((resolve) => {
  setTimeout(() => {
    console.log('任务执行完毕', name);
    resolve();
  }, Math.random() * 1000);
});

function limit(arr, limit = 3) {
  const task = arr.splice(0, limit).map((fun, index) => fun().then(() => index));
  let promise = Promise.race(task);
  for (let fun of arr) {
    promise = promise.then((index) => {
      task[index] = fun().then(() => index);
      return Promise.race(task);
    });
  }
  return promise.then(() => Promise.all(task));
}

limit([1,2,3,4,5,6,76,7,8,9,].map((value) => {
  return () => asyncFun(`任务${value}`);
})).then(() => console.log('执行完毕'));